﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using System.Collections;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;


namespace MS.InformationManagement.BLL.DistribucionRadicado
{
    class RecibirRadicadoDistribuido : IBaseCommand
    {
        #region [Private Fields]
        
        private Guid _recibido;
        private string _userName;
        private string _idRadicado;
        #endregion

        #region [Constructors]


        public RecibirRadicadoDistribuido(Guid recibido,string idRadicado, string userName)
        {
            _recibido = recibido;
            _idRadicado=idRadicado;
            _userName = userName;

        }

        #endregion

        public void Execute()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite ElevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb())
                    {
                        SPList targetList = null;
                        targetList = ElevatedSite.Lists.GetList(_recibido, false);
                        ElevatedSite.AllowUnsafeUpdates = true;
                        SPQuery query = new SPQuery();
                        query.ViewAttributes = "Scope='RecursiveAll'";
                        query.Query = string.Concat("<Where>",
                                                        "<Eq>",
                                                            "<FieldRef Name='_dlc_DocId' />",
                                                            "<Value Type='Text'>",
                                                              _idRadicado,
                                                            "</Value>",
                                                        "</Eq>",
                                                    "</Where>");
                        SPListItemCollection splc = targetList.GetItems(query);

                        if (splc.Count > 0)
                        {
                            SPListItem itemList = splc[0];
                            itemList[CustomSiteColumns.FIELD_DOCSET_INTERNAL_RECEPTION] = true;
                            itemList.Log(itemList[CustomSiteColumns.FIELD_DOCUMENT_SUBSECCION].ToString(), DateTime.Now, MS.InformationManagement.Support.Enums.TraceableOperation.Distribuido, _userName, "Radicado Distribuido");
                            itemList.Update();
                        }
                        ElevatedSite.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
        public void Undo()
        {

        }
    }
}
